package com.module.antiFake.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Tools;

import com.module.antiFake.vo.AntiFakeLBS;


/**
 * LBS服务数据处理类
 * @author mayb
 *
 */
public class AntiFakeLBSDao extends GenericHibernateDao<AntiFakeLBS,String>{
	
	private static AntiFakeLBSDao dao;
	AntiFakeLBSDao(){}
	
	public synchronized static AntiFakeLBSDao getInstance(){
		if(dao == null)
			dao = new AntiFakeLBSDao();
		return dao;
	}

	/**
	 * 按照经度与纬度定位地区
	 * 
	 * @param session
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public AntiFakeLBS findLBSByLatitudeAndLongitude(Session session ,Double longitude,Double  latitude){
		if(Tools.isValid(longitude) && Tools.isValid(longitude)){
			String  lbs = "SQRT(POW( ABS( X(location)-X(GeomFromText('POINT("+longitude+" "+latitude+")'))), 2)+" +
			" POW(ABS(Y(location)-Y(GeomFromText('POINT("+longitude+" "+latitude+")'))), 2 ))";
	String hql = " FROM AntiFakeLBS  where  "+lbs+" <1 order by "+lbs+"";
	Query query = session.createQuery(hql);
	List<AntiFakeLBS> list = query.list();
	if(!list.isEmpty() && list.size()>0){
		return list.get(0);
	}
		}
			return null;
		
		
	}

	/**
	 * 省市列表,jdbc主要是速度更快，不用load blob字段
	 * @param session
	 * @return
	 */
	public List<AntiFakeLBS> getLBSTree(Session session) {
		Statement stmt = null;
		List<AntiFakeLBS> result = new ArrayList<AntiFakeLBS>();
		ResultSet rs = null;
		List<AntiFakeLBS> rsList = new ArrayList<AntiFakeLBS>();
		try {
			Connection conn = session.connection();
			String sql = "SELECT i_id,c_province,c_city,c_county FROM antifake_lbs ";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				AntiFakeLBS antiFakeLBS = new AntiFakeLBS();
				antiFakeLBS.setId(rs.getInt("i_id"));
				antiFakeLBS.setCity(rs.getString("c_city"));
				antiFakeLBS.setCounty(rs.getString("c_county"));
				antiFakeLBS.setProvince(rs.getString("c_province"));
				rsList.add(antiFakeLBS);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			close(stmt);
			  close(rs);
			  close(rs);
		}
		changeToLBSTree(rsList,result);
		return result;
	}

	/**
	 * 把数据集列表转为省市区树状
	 * @param rsList
	 * @param result
	 */
	private void changeToLBSTree(List<AntiFakeLBS> rsList,
			List<AntiFakeLBS> result) {
		Map<String, Map<String,AntiFakeLBS>> pMap = new HashMap<String, Map<String,AntiFakeLBS>>();
		for(AntiFakeLBS antiFakeLBS : rsList){
			//省map
			Map<String, AntiFakeLBS> cMap = null;
			if(pMap.containsKey(antiFakeLBS.getProvince())){
				cMap = pMap.get(antiFakeLBS.getProvince());
			}else{
				cMap = new HashMap<String, AntiFakeLBS>();
				pMap.put(antiFakeLBS.getProvince(), cMap);
			}
			
			//市map
			AntiFakeLBS cityLbs = null;
			if(cMap.containsKey(antiFakeLBS.getCity())){
				cityLbs = cMap.get(antiFakeLBS.getCity());
			}else{
				cityLbs = new AntiFakeLBS();
				cityLbs.setAreaName(antiFakeLBS.getCity());
				cMap.put(antiFakeLBS.getCity(), cityLbs);
			}
			
			antiFakeLBS.setAreaName(antiFakeLBS.getCounty());
			cityLbs.getChildren().add(antiFakeLBS);
			
			
		}
		//把Map转为List
		for(Map.Entry<String, Map<String,AntiFakeLBS>> item : pMap.entrySet()){
			AntiFakeLBS provinceLBS = new AntiFakeLBS();
			provinceLBS.setAreaName(item.getKey());
			for(Map.Entry<String, AntiFakeLBS> cityItem : item.getValue().entrySet()){
				provinceLBS.getChildren().add(cityItem.getValue());
			}
			result.add(provinceLBS);
		}
	}

	public String getAddressById(Session session, Integer salesAreaId) {
		AntiFakeLBS antiFakeLBS = getUniqueByProperty(session, "id", salesAreaId);
		String result = "";
		if(antiFakeLBS != null){
			result = antiFakeLBS.getProvince()+antiFakeLBS.getCity()+antiFakeLBS.getCounty();
		}
		return result;
	}


}
